package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;


@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

   /*
   *
   * 添加购物车
   * */

    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //对象转换，把数据封装到购物车实体类对象中去数据库中操作
        ShoppingCart shoppingCart = new ShoppingCart();
        //拷贝属性
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //只能查询自己的购物车数据，通过用户id来限制
        shoppingCart.setUserId(BaseContext.getCurrentId());

        //判断当前商品是否在购物车中
        List<ShoppingCart>shoppingCartList = shoppingCartMapper.list(shoppingCart);

        if (shoppingCartList != null && shoppingCartList.size() ==1){
            //如果存在，就更新数量  +1
            shoppingCart =shoppingCartList.get(0);//获取到第一条数据
            //通过set给原有的数量+1
            shoppingCart.setNumber(shoppingCart.getNumber()+1);
            //通过Mapper层，修改数据库
            shoppingCartMapper.updateNumberById(shoppingCart);//updateNumberById：根据id修改数量
        }else {
            //不存在  插入数据，设置数量为 1

            //判断当前添加到购物车的是菜品还是套餐
            Long dishId = shoppingCartDTO.getDishId();
            //通过id判断添加进来的数据是不是菜品
            if (dishId !=null){
                //进if 为菜品    通过id查询数据。并创建菜品实体类对象
                Dish dish = dishMapper.getById(dishId);
                //补全购物车中的属性   通过set方法把dish中含有shoppingCart属性的数据封装进去
                shoppingCart.setName(dish.getName());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());



            }else {
                //进else  为套餐
                Long setmealId = shoppingCartDTO.getSetmealId();
                //通过id查询数据库，并创建套餐实体类对象
                Setmeal setmeal = setmealMapper.getById(setmealId);
                //补全购物车数据。
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setImage(setmeal.getImage());
            }
            //公共数据，一次性添加
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1);

            //调用insert方法，把数据添加到购物车中吗
            shoppingCartMapper.insert(shoppingCart);
        }
    }



    /*
    * 查看购物车
    * */
    @Override
    public List<ShoppingCart> showShoppingCart() {
        //通过拦截器获得用户id
        Long userId = BaseContext.getCurrentId();
        //通过构造器获得实体类，并把userId传入进去
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(userId)
                .build();
        //把查询出来的数据封装到list中
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        //返回给controller
        return list;
    }



    /*
    * 清空购物车
    * */
    @Override
    public void cleanShoppingCart() {
        //通过拦截器获得用户id
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.deleteByUserId(userId);
    }



    /*
    * 删除购物车中一个商品
    * */
    @Override
    public void deleteCart(ShoppingCartDTO shoppingCartDTO) {
        //对象转换，获得实体类对象
        ShoppingCart shoppingCart = new ShoppingCart();
        //属性拷贝
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //通过拦截器获得用户id
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //调用mapper层方法
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        //获取到第一条数据
        ShoppingCart shoppingCart1 = list.get(0);
        //判断单品订单数量是否大于1
        if (shoppingCart1.getNumber()>1){
            //进入if，大于1.。数量减1份
            shoppingCart1.setNumber(shoppingCart1.getNumber()-1);
            //调用mapper层方法，把数据存入到数据库
            shoppingCartMapper.updateNumberById(shoppingCart1);
        }else {
            //进入else  ，小于1  ，删除单品
            shoppingCartMapper.deleteCart(shoppingCart1);
        }
    }
}
